<template>
  <div class="login">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="90px" class="demo-ruleForm">
      <el-form-item label="用户名:" prop="userName" style="padding-top:50px;">
        <el-input v-model="ruleForm.userName" autocomplete="off" style="width:80%;" prefix-icon="el-icon-user-solid"></el-input>
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off" style="width:80%;" prefix-icon="el-icon-lock"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('ruleForm')">登录</el-button>
        <el-button type="primary" @click="register('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'login',
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userName: '',//用户名
        password: '',//密码
      },
      rules: {
        userName: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('登录成功')
        } else {
          console.log('登录失败');
          return false;
        }
      });
      this.$refs[formName].resetFields();
    },
    register(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('注册成功');
        } else {
          console.log('注册失败');
          return false;
        }
      });
      this.$refs[formName].resetFields();
    }
  },
  created () {

  }
}
</script>
<style lang="less" scoped>
.login {
  width: 100%;
  height: 100vh;
  position: relative;
  background: linear-gradient(#A770EF,#CF8BF3,#FDB99B);
  .el-form {
    width: 500px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background: #fff;
    box-shadow: 0px 1px 5px 3px #eee;
  }
}
</style>

